package org.xinyin025.sinaweibodemo;

import com.weibo.sdk.android.Oauth2AccessToken;
import com.weibo.sdk.android.Weibo;
import com.weibo.sdk.android.WeiboAuthListener;
import com.weibo.sdk.android.WeiboDialogError;
import com.weibo.sdk.android.WeiboException;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.text.TextUtils;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 微博发布测试
 * @author 蓝骑士(xinyin025)
 * @version 1.0
 */
public class MainActivity extends Activity implements OnClickListener {
	Button get_oauth_btn,send_msg_btn,send_msg_with_pic_btn,send_msg_with_url_pic_btn;
	TextView oauth_info_tv;
	Weibo mWeibo ;
	
	public static Oauth2AccessToken accessToken;
	
//	final String CONSUMER_KEY = "4252384308";
	final String CONSUMER_KEY = "966056985";
//	final String REDIRECT_URL = "http://weibo.com/xinyin025";
	final String REDIRECT_URL = "http://www.sina.com";
	SharedPreferences pre;
	Editor editor;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		pre = getSharedPreferences("Setting", MODE_PRIVATE);
		initView();
		mWeibo = Weibo.getInstance(CONSUMER_KEY, REDIRECT_URL);
	}
	
	/**
	 * 初始化View
	 */
	void initView(){
		get_oauth_btn = (Button) findViewById(R.id.get_oauth);
		send_msg_btn = (Button) findViewById(R.id.send_msg);
		send_msg_with_pic_btn = (Button) findViewById(R.id.send_msg_with_pic);
		oauth_info_tv = (TextView) findViewById(R.id.oauth_info);
		
		get_oauth_btn.setOnClickListener(this);
		send_msg_btn.setOnClickListener(this);
		send_msg_with_pic_btn.setOnClickListener(this);
	}
	

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

	@Override
	public void onClick(View v) {
		Intent intent = null;
		switch (v.getId()) {
		case R.id.get_oauth:
			 mWeibo.authorize(MainActivity.this, new AuthDialogListener());
			break;
		case R.id.send_msg:
			if(TextUtils.isEmpty(pre.getString("token", ""))){
				Toast.makeText(MainActivity.this, "请先获取授权!", Toast.LENGTH_SHORT).show();
				return;
			}
			intent = new Intent(MainActivity.this,SendMsg.class);
			startActivity(intent);
			break;
		case R.id.send_msg_with_pic:
			if(TextUtils.isEmpty(pre.getString("token", ""))){
				Toast.makeText(MainActivity.this, "请先获取授权!", Toast.LENGTH_SHORT).show();
				return;
			}
			intent = new Intent(MainActivity.this,SendMsgWithPic.class);
			startActivity(intent);
			break;

		default:
			break;
		}
	}

	class AuthDialogListener implements WeiboAuthListener{

		@Override
		public void onCancel() {
			Toast.makeText(getApplicationContext(), "Auth cancel : ",
					Toast.LENGTH_SHORT).show();
		}

		@Override
		public void onComplete(Bundle values) {
			String token = values.getString("access_token");
			String expires_in = values.getString("expires_in");
			editor.putString("token", token);
			editor.putString("expires_in", expires_in);
			editor.commit();
			// MainActivity.accessToken = new Oauth2AccessToken(token, expires_in);
			oauth_info_tv.setText("access token:\n"+token+"\nexpires_in:\n"+expires_in);
		}

		@Override
		public void onError(WeiboDialogError e) {
			Toast.makeText(getApplicationContext(),
					"Auth error : " + e.getMessage(), Toast.LENGTH_SHORT).show();
		}

		@Override
		public void onWeiboException(WeiboException e) {
			Toast.makeText(getApplicationContext(),
					"Auth exception : " + e.getMessage(), Toast.LENGTH_SHORT)
					.show();
		}
	}
}
